#include "salesdetails.h"
#include "ui_salesdetails.h"
#include <QDateTime>
#include <QKeyEvent>
#include <QDebug>
#include <QMessageBox>
#include "inquiresql.h"
#include "calendar.h"
#include <QCheckBox>

SalesDetails::SalesDetails(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SalesDetails)
{
    ui->setupUi(this);
    this->setLayout(ui->MainLayout);

    //设置自定义的鼠标指针图案
    QCursor *myCursor= new QCursor(QPixmap(":/Mouse/Image/mouse/mouse.png"),15,10);
    setCursor(*myCursor);

    clearTableWidgetSalesOrderForm = false;

    ui->tableWidgetSalesDetail->verticalHeader()->setHidden(true);
    ui->tableWidgetSalesList->verticalHeader()->setHidden(true);
    ui->tableWidgetSalesOrderForm->verticalHeader()->setHidden(true);

    //设置列宽可拖动
//    ui->tableWidgetSalesDetail->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
//    ui->tableWidgetSalesList->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
//    ui->tableWidgetSalesOrderForm->horizontalHeader()->setResizeMode(QHeaderView::Interactive);

    ui->tableWidgetSalesDetail->setStyleSheet("\
    background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 1, \
        stop: 0 rgba(255, 0, 0, 30%), stop: 0.2 rgba(255, 128, 0, 30%), \
        stop: 0.4 rgba(255, 255, 0, 30%), stop: 0.6 rgba(0, 255, 0, 30%), \
        stop: 0.8 rgba(0, 128, 255, 30%), stop: 1 rgba(128, 0, 255, 30%)); ");
    ui->tableWidgetSalesList->setStyleSheet("\
    background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 1, \
        stop: 0 rgba(255, 0, 0, 30%), stop: 0.2 rgba(255, 128, 0, 30%), \
        stop: 0.4 rgba(255, 255, 0, 30%), stop: 0.6 rgba(0, 255, 0, 30%), \
        stop: 0.8 rgba(0, 128, 255, 30%), stop: 1 rgba(128, 0, 255, 30%)); ");
    ui->tableWidgetSalesOrderForm->setStyleSheet("\
    background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 1, \
        stop: 0 rgba(255, 0, 0, 30%), stop: 0.2 rgba(255, 128, 0, 30%), \
        stop: 0.4 rgba(255, 255, 0, 30%), stop: 0.6 rgba(0, 255, 0, 30%), \
        stop: 0.8 rgba(0, 128, 255, 30%), stop: 1 rgba(128, 0, 255, 30%)); ");

    ui->tabWidget->setStyleSheet("\
        QTabBar::tab {\
            color: rgb(84,2,119);\
            background-image: url(:/attribute/Image/attribute/wood.jpg); \
            border: 2px solid rgb(68,66,64);\
            border-bottom-color: rgb(68,66,64); \
            border-top-left-radius: 24px;\
            border-top-right-radius: 24px;\
            max-height: 25px;\
            max-width: 40px;\
            min-width: 20ex;\
            padding: 2px;\
            } \
        QTabWidget::tab-bar {\
            alignment: left;\
            } \
        QTabBar::tab:!selected {\
            margin-top: 5px; \
            }\
        QTabBar::tab:selected {\
            color: rgb(255,0,128); \
            }\
        ");

        //center

    setQtableWidgetHeaderStyle();

    ui->lineEditDateStart->installEventFilter(this);
    ui->lineEditDateEnd->installEventFilter(this);

    m_calendar = new Calendar(this);
    m_calendar->hide();

    connect(m_calendar, SIGNAL(signalUpdateLineEditDate(QString)),
            this, SLOT(slotUpdateLineEditDate(QString)));

    m_LineEdit = -1;
    LineEditEnable = true;

    //初始化显示当天的进货订单
    initLineEditTime();
}

SalesDetails::~SalesDetails()
{
    delete ui;
}

bool SalesDetails::eventFilter(QObject *target, QEvent *event)
{
    if ( target == ui->lineEditDateEnd || target == ui->lineEditDateStart )
    {
        //鼠标左键释放事件
        if (event->type() == QEvent::MouseButtonRelease)
        {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
            if (mouseEvent->button() == Qt::LeftButton)
            {
                QString time;
                if (target == ui->lineEditDateEnd)
                {
                    m_LineEdit = end;
                    time = ui->lineEditDateEnd->text();
                }
                else if (target == ui->lineEditDateStart)
                {
                    m_LineEdit = start;
                    time = ui->lineEditDateStart->text();
                }
                if (LineEditEnable)
                {
                    m_calendar->setSelectedCurrentDate(time);
                    m_calendar->setWindowFlags(
                                Qt::FramelessWindowHint | Qt::Dialog);

                    if (m_LineEdit == start)
                    {
                        QPoint GlobalPoint(ui->lineEditDateStart->mapToGlobal(QPoint(0, 0)));
                        int x = GlobalPoint.x();
                        int y = GlobalPoint.y();
                        m_calendar->setGeometry(x , y + 35, 150, 130);
                    }
                    else if (m_LineEdit == end)
                    {
                        QPoint GlobalPoint(ui->lineEditDateEnd->mapToGlobal(QPoint(0, 0)));
                        int x = GlobalPoint.x();
                        int y = GlobalPoint.y();
                        m_calendar->setGeometry(x , y + 35, 150, 130);
                    }
                    m_calendar->show();
                }
            }
        }
    }
    return QWidget::eventFilter(target, event);
}

void SalesDetails::displayTableWidgetSalesOrderForm()
{
    m_saleOrderFormList.clear();

    clearTableWidgetSalesOrderForm = true;
    ui->tableWidgetSalesOrderForm->clear();

    if (LineEditEnable)
        m_saleOrderFormList = m_sql->InquireSalesOrderForm(ui->lineEditDateStart->text(),
                        ui->lineEditDateEnd->text(),
                        ui->comboBoxInquireManner->currentIndex(),
                        ui->lineEditInquireInformation->text());
    else
        m_saleOrderFormList = m_sql->InquireSalesOrderForm("all", "all",
                        ui->comboBoxInquireManner->currentIndex(),
                        ui->lineEditInquireInformation->text());

    setQtableWidgetHeaderStyle();

    //显示会员办卡/充值信息
    ui->tableWidgetSalesOrderForm->setRowCount(m_saleOrderFormList.count());

    for(int i = 0; i < m_saleOrderFormList.count(); ++i)
    {
        QTableWidgetItem *saleOrderFormNumber      = new QTableWidgetItem(
                    m_saleOrderFormList.at(i).get_saleOrderFormNumber());
        QTableWidgetItem *memberCardNumber         = new QTableWidgetItem(
                    m_saleOrderFormList.at(i).get_memberCardNumber());
        QTableWidgetItem *memberName         = new QTableWidgetItem(
                    m_saleOrderFormList.at(i).get_memberName());
        QTableWidgetItem *quantity              = new QTableWidgetItem(
                    m_saleOrderFormList.at(i).get_quantity());
        QTableWidgetItem *totalSales        = new QTableWidgetItem(
                    completionPrice(m_saleOrderFormList.at(i).get_totalSales()));
        QTableWidgetItem *integration                 = new QTableWidgetItem(
                    m_saleOrderFormList.at(i).get_integration());
        QTableWidgetItem *memberPayment      = new QTableWidgetItem(
                    completionPrice(m_saleOrderFormList.at(i).get_memberPayment()));
        QTableWidgetItem *cashPayment         = new QTableWidgetItem(
                    completionPrice(m_saleOrderFormList.at(i).get_cashPayment()));
        QTableWidgetItem *CUPPayment              = new QTableWidgetItem(
                    completionPrice(m_saleOrderFormList.at(i).get_CUPPayment()));
        QTableWidgetItem *staffName        = new QTableWidgetItem(
                    m_saleOrderFormList.at(i).get_staffName());
        QTableWidgetItem *salesDate                 = new QTableWidgetItem(
                    m_saleOrderFormList.at(i).get_salesDate());

        //设置对齐方式
        saleOrderFormNumber->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        memberCardNumber->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        quantity->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        totalSales->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter );
        integration->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        memberPayment->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter );
        cashPayment->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter );
        CUPPayment->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter );
        staffName->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        salesDate->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );

        saleOrderFormNumber->setFont(QFont("DejaVu Sans", 11, 2, false));
        memberCardNumber->setFont(QFont("DejaVu Sans", 11, 2, false));
        memberName->setFont(QFont("DejaVu Sans", 11, 2, false));
        quantity->setFont(QFont("DejaVu Sans", 11, 2, false));
        totalSales->setFont(QFont("DejaVu Sans", 11, 2, false));
        integration->setFont(QFont("DejaVu Sans", 11, 2, false));
        memberPayment->setFont(QFont("DejaVu Sans", 11, 2, false));
        cashPayment->setFont(QFont("DejaVu Sans", 11, 2, false));
        CUPPayment->setFont(QFont("DejaVu Sans", 11, 2, false));
        staffName->setFont(QFont("DejaVu Sans", 11, 2, false));
        salesDate->setFont(QFont("DejaVu Sans", 11, 2, false));

        ui->tableWidgetSalesOrderForm->setItem(i, 0, saleOrderFormNumber);
        ui->tableWidgetSalesOrderForm->setItem(i, 1, memberCardNumber);
        ui->tableWidgetSalesOrderForm->setItem(i, 2, memberName);
        ui->tableWidgetSalesOrderForm->setItem(i, 3, quantity);
        ui->tableWidgetSalesOrderForm->setItem(i, 4, totalSales);
        ui->tableWidgetSalesOrderForm->setItem(i, 5, integration);
        ui->tableWidgetSalesOrderForm->setItem(i, 6, memberPayment);
        ui->tableWidgetSalesOrderForm->setItem(i, 7, cashPayment);
        ui->tableWidgetSalesOrderForm->setItem(i, 8, CUPPayment);
        ui->tableWidgetSalesOrderForm->setItem(i, 9, staffName);
        ui->tableWidgetSalesOrderForm->setItem(i, 10, salesDate);
    }

    ui->tableWidgetSalesOrderForm->setCurrentItem(
                ui->tableWidgetSalesOrderForm->item(m_saleOrderFormList.count() - 1, 0));

    //设置与文字相匹配的单元格大小
    ui->tableWidgetSalesOrderForm->resizeColumnsToContents();
    ui->tableWidgetSalesOrderForm->resizeRowsToContents();

    setTableWidgetColor();
}

void SalesDetails::displayTableWidgetSalesDetails(QString salesOrderFormNumber)
{
    m_saleDetailList.clear();
    ui->tableWidgetSalesDetail->clear();

    m_saleDetailList = m_sql->InquireSalesDetail(salesOrderFormNumber);

    setQtableWidgetSalesDetailsHeaderStyle();

    ui->tableWidgetSalesDetail->setRowCount(m_saleDetailList.count());

    for(int i = 0; i < m_saleDetailList.count(); ++i)
    {
        QTableWidgetItem *commodityBarcode      = new QTableWidgetItem(
                    m_saleDetailList.at(i).get_commodityBarcode());
        QTableWidgetItem *commodityName         = new QTableWidgetItem(
                    m_saleDetailList.at(i).get_commodityName());
        QTableWidgetItem *internalCode         = new QTableWidgetItem(
                    m_saleDetailList.at(i).get_internalCode());
        QTableWidgetItem *unitValue              = new QTableWidgetItem(
                    m_saleDetailList.at(i).get_unitNumber());
        QTableWidgetItem *sellingPrice        = new QTableWidgetItem(
                    completionPrice(m_saleDetailList.at(i).get_sellingPrice()));
        QTableWidgetItem *realDiscount                 = new QTableWidgetItem(
                    m_saleDetailList.at(i).get_realDiscount()+'%');
        QTableWidgetItem *realSellingPrice      = new QTableWidgetItem(
                    completionPrice(m_saleDetailList.at(i).get_realSellingPrice()));
        QTableWidgetItem *saleQuantity         = new QTableWidgetItem(
                    m_saleDetailList.at(i).get_saleQuantity());
        QTableWidgetItem *saleSum              = new QTableWidgetItem(
                    completionPrice(m_saleDetailList.at(i).get_saleSum()));

        QCheckBox *integration = new QCheckBox;
        integration->setStyleSheet("\
            QCheckBox:checked { color: green; }\
            QCheckBox::indicator { width: 18px; height: 18px; }\
            QCheckBox::indicator:checked \
            { image: url(:/CommoditySale/Image/CommoditySale/checkbox.gif);}\
            ");
        if (m_saleDetailList.at(i).get_integration())
        {
            integration->setEnabled(true);
            integration->setCheckState(Qt::Checked);
        }
        else
        {
            integration->setEnabled(false);
        }

        QTableWidgetItem *saleStaffName        = new QTableWidgetItem(
                    m_saleDetailList.at(i).get_saleStaffName());

        //设置对齐方式
        commodityBarcode->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        internalCode->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        unitValue->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        sellingPrice->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter );
        realDiscount->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        realSellingPrice->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter );
        saleQuantity->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        saleSum->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter );
        saleStaffName->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );

        //设置字体大小
        commodityBarcode->setFont(QFont("DejaVu Sans", 11, 2, false));
        commodityName->setFont(QFont("DejaVu Sans", 11, 2, false));
        internalCode->setFont(QFont("DejaVu Sans", 11, 2, false));
        unitValue->setFont(QFont("DejaVu Sans", 11, 2, false));
        sellingPrice->setFont(QFont("DejaVu Sans", 11, 2, false));
        realDiscount->setFont(QFont("DejaVu Sans", 11, 2, false));
        realSellingPrice->setFont(QFont("DejaVu Sans", 11, 2, false));
        saleQuantity->setFont(QFont("DejaVu Sans", 11, 2, false));
        saleSum->setFont(QFont("DejaVu Sans", 11, 2, false));
        saleStaffName->setFont(QFont("DejaVu Sans", 11, 2, false));

        ui->tableWidgetSalesDetail->setItem(i, 0, commodityBarcode);
        ui->tableWidgetSalesDetail->setItem(i, 1, commodityName);
        ui->tableWidgetSalesDetail->setItem(i, 2, internalCode);
        ui->tableWidgetSalesDetail->setItem(i, 3, unitValue);
        ui->tableWidgetSalesDetail->setItem(i, 4, sellingPrice);
        ui->tableWidgetSalesDetail->setItem(i, 5, realDiscount);
        ui->tableWidgetSalesDetail->setItem(i, 6, realSellingPrice);
        ui->tableWidgetSalesDetail->setItem(i, 7, saleQuantity);
        ui->tableWidgetSalesDetail->setItem(i, 8, saleSum);
        ui->tableWidgetSalesDetail->setCellWidget(i, 9, integration);
        ui->tableWidgetSalesDetail->setItem(i, 10, saleStaffName);
    }

    ui->tableWidgetSalesDetail->setCurrentItem(
                ui->tableWidgetSalesDetail->item(m_saleDetailList.count() - 1, 0));

    //设置与文字相匹配的单元格大小
    ui->tableWidgetSalesDetail->resizeColumnsToContents();
    ui->tableWidgetSalesDetail->resizeRowsToContents();

    setTableWidgetColor();
}


void SalesDetails::displayTableWidgetSalesList()
{
    m_saleDetailList.clear();
    ui->tableWidgetSalesList->clear();

    if (LineEditEnable)
        m_saleDetailList = m_sql->InquireSalesList(ui->lineEditDateStart->text(),
                        ui->lineEditDateEnd->text(),
                        ui->comboBoxInquireManner->currentIndex(),
                        ui->lineEditInquireInformation->text());
    else
        m_saleDetailList = m_sql->InquireSalesList("all", "all",
                        ui->comboBoxInquireManner->currentIndex(),
                        ui->lineEditInquireInformation->text());

    setQtableWidgetSalesListHeaderStyle();

    ui->tableWidgetSalesList->setRowCount(m_saleDetailList.count());

    for(int i = 0; i < m_saleDetailList.count(); ++i)
    {
        QTableWidgetItem *commodityBarcode      = new QTableWidgetItem(
                    m_saleDetailList.at(i).get_commodityBarcode());
        QTableWidgetItem *commodityName         = new QTableWidgetItem(
                    m_saleDetailList.at(i).get_commodityName());
        QTableWidgetItem *sellingPrice        = new QTableWidgetItem(
                    completionPrice(m_saleDetailList.at(i).get_sellingPrice()));
        QTableWidgetItem *realDiscount                 = new QTableWidgetItem(
                    m_saleDetailList.at(i).get_realDiscount()+'%');
        QTableWidgetItem *realSellingPrice      = new QTableWidgetItem(
                    completionPrice(m_saleDetailList.at(i).get_realSellingPrice()));
        QTableWidgetItem *saleQuantity         = new QTableWidgetItem(
                    m_saleDetailList.at(i).get_saleQuantity());
        QTableWidgetItem *saleSum              = new QTableWidgetItem(
                    completionPrice(m_saleDetailList.at(i).get_saleSum()));
        QTableWidgetItem *saleStaffName        = new QTableWidgetItem(
                    m_saleDetailList.at(i).get_saleStaffName());

        //设置对齐方式
        commodityBarcode->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        sellingPrice->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter );
        realDiscount->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        realSellingPrice->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter );
        saleQuantity->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        saleSum->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter );
        saleStaffName->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );

        //设置字体大小
        commodityBarcode->setFont(QFont("DejaVu Sans", 11, 2, false));
        commodityName->setFont(QFont("DejaVu Sans", 11, 2, false));
        sellingPrice->setFont(QFont("DejaVu Sans", 11, 2, false));
        realDiscount->setFont(QFont("DejaVu Sans", 11, 2, false));
        realSellingPrice->setFont(QFont("DejaVu Sans", 11, 2, false));
        saleQuantity->setFont(QFont("DejaVu Sans", 11, 2, false));
        saleSum->setFont(QFont("DejaVu Sans", 11, 2, false));
        saleStaffName->setFont(QFont("DejaVu Sans", 11, 2, false));

        ui->tableWidgetSalesList->setItem(i, 0, commodityBarcode);
        ui->tableWidgetSalesList->setItem(i, 1, commodityName);
        ui->tableWidgetSalesList->setItem(i, 2, sellingPrice);
        ui->tableWidgetSalesList->setItem(i, 3, realDiscount);
        ui->tableWidgetSalesList->setItem(i, 4, realSellingPrice);
        ui->tableWidgetSalesList->setItem(i, 5, saleQuantity);
        ui->tableWidgetSalesList->setItem(i, 6, saleSum);
        ui->tableWidgetSalesList->setItem(i, 7, saleStaffName);
    }

    ui->tableWidgetSalesList->setCurrentItem(
                ui->tableWidgetSalesList->item(m_saleDetailList.count() - 1, 0));

    //设置与文字相匹配的单元格大小
    ui->tableWidgetSalesList->resizeColumnsToContents();
    ui->tableWidgetSalesList->resizeRowsToContents();

    setTableWidgetColor();
}

void SalesDetails::initLineEditTime()
{
    ui->lineEditDateStart->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd"));
    ui->lineEditDateEnd->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd"));
    ui->tableWidgetSalesOrderForm->clear();
    ui->tableWidgetSalesOrderForm->setRowCount(0);
    clearTableWidgetSalesOrderForm = true;
    ui->tableWidgetSalesDetail->clear();
    ui->tableWidgetSalesDetail->setRowCount(0);
    ui->tableWidgetSalesList->clear();
    ui->tableWidgetSalesList->setRowCount(0);

    setQtableWidgetHeaderStyle();
    setQtableWidgetSalesDetailsHeaderStyle();
}

void SalesDetails::slotUpdateLineEditDate(QString date)
{
    if (m_LineEdit == start)
    {
        ui->lineEditDateStart->setText(date);
        return;
    }
    if (m_LineEdit == end)
    {
        ui->lineEditDateEnd->setText(date);
        return;
    }
}

void SalesDetails::on_pushButtonInquire_clicked()
{
    int startYear = ui->lineEditDateStart->text().mid(0, 4).toInt();
    int startMonth = ui->lineEditDateStart->text().mid(5, 2).toInt();
    int startDay = ui->lineEditDateStart->text().mid(8, 2).toInt();

    int endYear = ui->lineEditDateEnd->text().mid(0, 4).toInt();
    int endMonth = ui->lineEditDateEnd->text().mid(5, 2).toInt();
    int endDay = ui->lineEditDateEnd->text().mid(8, 2).toInt();

    if ( (startYear > endYear) || (startYear == endYear && startMonth > endMonth)
         || (startYear == endYear && startMonth == endMonth && startDay > endDay) )
    {
        QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."),
                                 tr("\n开始日期大于结束日期，请重新设置") );
    }
    else
    {
        displayTableWidgetSalesOrderForm();
        displayTableWidgetSalesList();
    }
}

void SalesDetails::on_comboBox_activated(int index)
{
    QDateTime currentTime = QDateTime::currentDateTime();//获取系统现在的时间
//    QDateTime startTime, endTime;
    QString startTimeString, endTimeString;
    switch (index)
    {
        case 0 :  //今天
            startTimeString = currentTime.toString("yyyy-MM-dd");
            endTimeString = startTimeString;
            break;
        case 1 :  //昨天
            startTimeString = currentTime.addDays(-1).toString("yyyy-MM-dd");
            endTimeString = startTimeString;
            break;
        case 2 :  //本月
            startTimeString = currentTime.toString("yyyy-MM-dd").replace(8, 2, "01");
            endTimeString = currentTime.toString("yyyy-MM-dd");
            break;
        case 3 :  //上月
            startTimeString = currentTime.addMonths(-1).toString("yyyy-MM-dd").replace(8, 2, "01");
            endTimeString = currentTime.addDays(currentTime.toString("d").toInt() * -1)
                    .toString("yyyy-MM-dd");
            break;
        case 4 :  //今年
            startTimeString = currentTime.toString("yyyy-MM-dd").replace(5, 5, "01-01");
            endTimeString = currentTime.toString("yyyy-MM-dd");
            break;
        case 5 :  //去年
            startTimeString = currentTime.addYears(-1).toString("yyyy-MM-dd").replace(5, 5, "01-01");
            endTimeString = currentTime.addYears(-1).toString("yyyy-MM-dd").replace(5, 5, "12-31");
            break;
        case 6 :  //全部
            ui->lineEditDateStart->setText(tr("全部"));
            ui->lineEditDateEnd->setText(tr("全部"));
            ui->lineEditDateStart->setDisabled(true);
            ui->lineEditDateEnd->setDisabled(true);
            LineEditEnable = false;
            return;
        default :
            break;
    }
    ui->lineEditDateStart->setDisabled(false);
    ui->lineEditDateEnd->setDisabled(false);
    LineEditEnable = true;
    ui->lineEditDateStart->setText(startTimeString);
    ui->lineEditDateEnd->setText(endTimeString);
}

void SalesDetails::setQtableWidgetHeaderStyle()
{
    QStringList headers;
    headers << tr("消费单号") << tr("会员卡号") << tr("会员姓名") << tr("数量")
            << tr("销售金额") << tr("积分")     << tr("会员支付") << tr("现金支付")
            << tr("银联支付") << tr("操作人员") << tr("消费日期") ;

    ui->tableWidgetSalesOrderForm->setColumnCount(11);
    ui->tableWidgetSalesOrderForm->setHorizontalHeaderLabels(headers);

    for (int i = 0; i < 11; ++i)
    {
        ui->tableWidgetSalesOrderForm
                ->horizontalHeaderItem(i)->setFont(QFont("Helvetica", 12, 2, false));
        ui->tableWidgetSalesOrderForm
                ->horizontalHeaderItem(i)->setTextColor(QColor(200,111,30));
    }
}

void SalesDetails::setQtableWidgetSalesDetailsHeaderStyle()
{
    QStringList headers;
    headers << tr("商品编号") << tr("商品名称") << tr("商品条码") << tr("单价")
            << tr("原价") << tr("折扣")     << tr("售价") << tr("数量")
            << tr("金额") << tr("积分") << tr("销售人员") ;

    ui->tableWidgetSalesDetail->setColumnCount(11);
    ui->tableWidgetSalesDetail->setHorizontalHeaderLabels(headers);

    for (int i = 0; i < 11; ++i)
    {
        ui->tableWidgetSalesDetail
                ->horizontalHeaderItem(i)->setFont(QFont("Helvetica", 12, 2, false));
        ui->tableWidgetSalesDetail
                ->horizontalHeaderItem(i)->setTextColor(QColor(200,111,30));
    }
}

void SalesDetails::setQtableWidgetSalesListHeaderStyle()
{
    QStringList headers;
    headers << tr("商品编号") << tr("商品名称")
            << tr("原价") << tr("折扣")     << tr("售价") << tr("数量")
            << tr("金额") << tr("销售人员") ;

    ui->tableWidgetSalesList->setColumnCount(8);
    ui->tableWidgetSalesList->setHorizontalHeaderLabels(headers);

    for (int i = 0; i < 8; ++i)
    {
        ui->tableWidgetSalesList
                ->horizontalHeaderItem(i)->setFont(QFont("Helvetica", 12, 2, false));
        ui->tableWidgetSalesList
                ->horizontalHeaderItem(i)->setTextColor(QColor(200,111,30));
    }
}

void SalesDetails::setTableWidgetColor()
{
    for (int i = 0; i < ui->tableWidgetSalesOrderForm->rowCount(); ++i)

    {
        if (i % 2 == 0)
        {
            for (int j = 0; j < ui->tableWidgetSalesOrderForm->columnCount(); ++j)
            {
                QTableWidgetItem *item = ui->tableWidgetSalesOrderForm->item(i,j);
                if (item)
                {
                    const QColor color = QColor(252, 222, 156);
                    item->setBackgroundColor(color);
                }
            }
        }
    }
}

void SalesDetails::setTableWidgetSalesDetailsColor()
{
    for (int i = 0; i < ui->tableWidgetSalesDetail->rowCount(); ++i)

    {
        if (i % 2 == 0)
        {
            for (int j = 0; j < ui->tableWidgetSalesDetail->columnCount(); ++j)
            {
                QTableWidgetItem *item = ui->tableWidgetSalesDetail->item(i,j);
                if (item)
                {
                    const QColor color = QColor(252, 222, 156);
                    item->setBackgroundColor(color);
                }
            }
        }
    }
}

void SalesDetails::setTableWidgetSalesListColor()
{
    for (int i = 0; i < ui->tableWidgetSalesList->rowCount(); ++i)
    {
        if (i % 2 == 0)
        {
            for (int j = 0; j < ui->tableWidgetSalesList->columnCount(); ++j)
            {
                QTableWidgetItem *item = ui->tableWidgetSalesList->item(i,j);
                if (item)
                {
                    const QColor color = QColor(252, 222, 156);
                    item->setBackgroundColor(color);
                }
            }
        }
    }
}

QString SalesDetails::completionPrice(const QString &price)
{
    QString number = price;
    if (number.isEmpty() || number.toDouble() == 0)
    {
        number.append("0.00");
        return number;
    }
    if (number.contains(".", Qt::CaseInsensitive))
    {
        if (number.left(1) == ".")
        {
            number.replace(0, 1, "0.");
            return number;
        }
        if (number.right(1) == ".")
        {
            number.append("00");
            return number;
        }
        if (number.right(2).contains(".", Qt::CaseInsensitive))
        {
            number.append("0");
            return number;
        }
    }
    else
    {
        number.append(".00");
        return number;
    }

    return NULL;
}

void SalesDetails::on_tableWidgetSalesOrderForm_currentCellChanged(int currentRow, int, int , int )
{
//    (ui->tableWidgetSalesOrderForm->item(currentRow, 0)->text());
//    qDebug() << "pan";
//    qDebug() << ui->tableWidgetSalesOrderForm->item(currentRow, 0)->text();
    if (!clearTableWidgetSalesOrderForm)
        displayTableWidgetSalesDetails(
                ui->tableWidgetSalesOrderForm->item(currentRow, 0)->text());
    else
        clearTableWidgetSalesOrderForm = false;
}
